	dojo.require("dojo.parser");
	dojo.require("dijit.form.Form");
	dojo.require("dijit.form.Button");
	dojo.require("dijit.form.TextBox");
	dojo.require("dijit.form.ValidationTextBox");
	dojo.require("dojox.grid.EnhancedGrid");
	dojo.require("dojox.grid.enhanced.plugins.Pagination");
	dojo.require("dojox.data.QueryReadStore");
	dojo.require("dijit._Widget"); 
	dojo.require("dijit.Toolbar");
	dojo.require("dijit.ToolbarSeparator");
	dojo.require("dijit.ToolbarSeparator");
	dojo.ready(function() {
	   var store = new dojox.data.QueryReadStore({
		   id:"ent_store",
		   url: contextPath + "/ent/list.do",
			requestMethod : "get"	
		});
		//store.fetch();
		/*set up layout*/
		var layout = [ [ {
			name : '企业编号',
			field : 'entCode',
			width : "100px"
		}, {
			name : '企业名称',
			field : 'entName',
			width : "230px"
		}, {
			name : '联系电话',
			field : 'entPhone',
			width : "100px"
		}, {
			name : '地址',
			field : 'entAddr',
			width : "500px"
		} ] ];

		/*create a new grid:*/
		var grid = new dojox.grid.EnhancedGrid({
			id : 'ent_grid',
			query : {},
			store : store,
			structure : layout,
			rowSelector : '20px',
			plugins : {
				pagination : {
					pageSizes : [ "10", "20", "50", "100", "All" ],
					description : true,
					sizeSwitch : true,
					pageStepper : true,
					gotoButton : true,
					defaultPageSize : 20,
					maxPageStep : 6,
					position : "bottom"
				}
			}
		}, document.createElement('div'));

		/*append the new grid to the div*/
		dojo.byId("ent_grid").appendChild(grid.domNode);

		/*Call startup() to render the grid*/
		grid.startup();
		dojo.connect(dijit.byId("btn_add"), "onClick", "uf_add");
		dojo.connect(dijit.byId("btn_edit"), "onClick", "uf_edit");
		dojo.connect(dijit.byId("btn_save"), "onClick", "uf_save");
		dojo.connect(dijit.byId("btn_cancle"), "onClick", "uf_cancle");
	});

	function uf_add() {
		dijit.byId("myForm").reset();
		dijit.byId("oper").set("value","add");
		dijit.byId("u_dialog").show();
	}
	
	function uf_edit() {
		var grid = dijit.byId("ent_grid");
		var items = grid.selection.getSelected();
		if(items.length == 0){
			alert("请选择要编辑的记录");
		  	return;
		}
	  	var entid = grid.store.getValue(items[0], "entId");
		var myForm = dijit.byId("myForm");
		myForm.reset();
		dojo.xhrGet({
			url : contextPath + "/ent/load.do",
			handleAs:"json",
			content:{entid:entid},
			load : function(data) {
				dijit.byId("u_dialog").show();
				myForm.setValues(data);
				dijit.byId("oper").set("value","mod");
			},
			error : function(error, arg) {
				alert(error.response.text);
			}
		});
	}
	
	
	function uf_save() {
		uf_submit();
	}
	
	function uf_submit() {
		var myForm  = dijit.byId("myForm");
		if (myForm.validate() == false) {
			return;
		}
		dojo.xhrPost({
			form : dojo.byId("myForm"),
			load : function(data) {
				var dialog = dijit.byId("u_dialog");
				dialog.hide();
				dijit.byId("ent_grid").render();
				//dijit.byId("ent_store").fetch({query:{t:Meth.random()}});
				
			},
			error : function(error, arg) {
				alert(error.response.text);
			}
		});
	}
	
	function uf_cancle() {
		dijit.byId("u_dialog").hide();
	}